<?php
/**
 * Copyright (c) 2018. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
 * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
 * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
 * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
 * Vestibulum commodo. Ut rhoncus gravida arcu.
 */

/**
 * Created by PhpStorm.
 * User: yf
 * Date: 2017/8/30
 * Time: 10:22
 */
namespace SDXApi\Controller;


use SDXApi\Service\OrderDistributionTimeService;
use Think\Controller;

class UpdateController extends Controller {
    public function __construct()
    {
        header("Content-type: text/html; charset=utf-8");
    }

    public function m(){

        $order = M("order_info")->where(array("order_id"=>4337523))->field("add_time,wdr_id,best_time,best_time_start")->find();
        $best_time_start = date('Y-m-d H:i', $order['add_time']+28800);
        dump($best_time_start);

        $best_time_start = date('Y-m-d H:i', $order['best_time_start'])."-".date('H:i', $order['best_time']);
        dump($best_time_start);

        $mode = M("warehouse_deliver_region")->where("id=".$order['wdr_id'])->getField("wdtype");


        $distributie_time = $this->distributie_time_list_new($mode,$order['wdr_id'],$order['add_time']+28800);

        $best_time_start = date('Y-m-d H:i', $distributie_time['start_time'])."-".date('H:i', $distributie_time['end_time']);
        dump($best_time_start);

        dump($order);
        dump($mode);
    }

    public function p(){
        set_time_limit(0);
        $data = I("get.data");
        if(empty($data)){
            $data = date('Y-m-d',time());
        }
        echo "<h1>访问格式为：http://api.shidexian.com/index.php?s=update/p/data/".$data."</h1>";

        $start = strtotime($data)-28800;
        $end = strtotime($data." 23:59:59")-28800;


        $indb = M("order_info")->field("best_time_start_initial,best_time_initial,address,order_id,order_sn,wdr_id,add_time")
            ->where("add_time > ".$start." AND add_time < ".$end." AND pay_status in (2,3) AND shipping_id = 3 AND best_time_start !=0")->select();

        echo "<table border=\"1\"><tr>
                <th>订单ID</th>
                <th>订单号</th>
                <th>地址</th>
                <th>下单时间</th>
                <th>当时可选最早时间1</th>
                <th>当时可选最早时间2</th>
                <th>实际选择实际1</th>
                <th>实际选择实际2</th>
                </tr>";
        foreach ($indb as $value){

            $mode = M("warehouse_deliver_region")->where("id=".$value['wdr_id'])->getField("wdtype");
            $distributie_time = $this->distributie_time_list_new($mode,$value['wdr_id'],$value['add_time']+28800);


            echo "<tr>";


            echo "<td>".$value['order_id']."</td>";
            echo "<td>".$value['order_sn']."</td>";
            echo "<td>".$value['address']."</td>";
            echo "<td>".date('Y-m-d H:i:s', $value['add_time']+28800)."</td>";

            echo "<td>".date('Y-m-d H:i', $distributie_time['start_time'])."</td>";
            echo "<td>".date('Y-m-d H:i', $distributie_time['end_time'])."</td>";
            echo "<td>".date('Y-m-d H:i', $value['best_time_start_initial'])."</td>";
            echo "<td>".date('Y-m-d H:i', $value['best_time_initial'])."</td>";
            echo "</tr>";
        }
        echo "</table>";
    }
    /**
     * 根据配送区域 获取可配送的时间段
     * @param null $mode
     * @param int $wdr_id
     * @return array
     */
    public function distributie_time_list_new($mode = null, $wdr_id=0,$nowtime=null){

        $distribution_time =  M("distribution_time")->field("id,start_time,end_time,closing_time")
            ->where(array("mode_id"=>$mode))->order("start_time asc")->select();
        $distribution_mode =  M("distribution_mode")->where(array("id"=>$mode))->getField("arrival_type");

        /**
         * 处理区域配送时间订单峰值、是否超时免单处理
         * Created By caicm
         */
//        if($wdr_id){
//            $distribution_model = new OrderDistributionTimeService();
//            $dis_time = $distribution_model->get_distributie_time($distribution_time, $distribution_mode, $wdr_id );
//            if(!empty($dis_time['today'])){
//                $timss = explode("-",$dis_time['today'][0]['value']);
//                return array(
//                    "start_time"=>$timss[0],
//                    "end_time"=>$timss[1]
//                );
//            }else{
//                $timss = explode("-",$dis_time['tomorrow'][0]['value']);
//                return array(
//                    "start_time"=>$timss[0],
//                    "end_time"=>$timss[1]
//                );
//            }
//        }

        //今日配送时间
        if($distribution_mode==1){
            //次日达区域，只显示今天配送去为空
            //明天配送时间
            foreach ($distribution_time as $value){
                $closing_time = str_replace(":","",$value['closing_time']);
                $start_time = str_replace(":","",$value['start_time']);
                if($start_time>$closing_time){
                    return array(
                        "start_time"=>strtotime(date("Y-m-d",$nowtime+86400)." ".$value['start_time'].":00"),
                        "end_time"=>strtotime(date("Y-m-d",$nowtime+86400)." ".$value['end_time'].":00")
                    );
                }
            }
        }else{
            //今日达区域，显示今天和明天
            foreach ($distribution_time as $value){
                $closing_time = str_replace(":","",$value['closing_time']);
                $start_time = str_replace(":","",$value['start_time']);
                $now_hour = date('H',$nowtime).date('i',$nowtime);
                if ($closing_time > $now_hour && $start_time > $closing_time) {
                    return array(
                        "start_time"=>strtotime(date("Y-m-d",$nowtime)." ".$value['start_time'].":00"),
                        "end_time"=>strtotime(date("Y-m-d",$nowtime)." ".$value['end_time'].":00")
                    );
                }
                if ($closing_time > $now_hour && $start_time < $closing_time) {
                    //开始时间大于截单时间，将时间定位次日
                    return array(
                        "start_time"=>strtotime(date("Y-m-d",$nowtime+86400)." ".$value['start_time'].":00"),
                        "end_time"=>strtotime(date("Y-m-d",$nowtime+86400)." ".$value['end_time'].":00")
                    );
                }
            }

            return array(
                "start_time"=>strtotime(date("Y-m-d",$nowtime+86400)." ".$distribution_time[0]['start_time'].":00"),
                "end_time"=>strtotime(date("Y-m-d",$nowtime+86400)." ".$distribution_time[0]['end_time'].":00")
            );
        }
        return null;
    }













    public function r(){
        //exit();
        $_REQUEST['time']=time()-10;
        $_REQUEST['mac']=md5("key4token".$_REQUEST['time']);
        $user_id = 1;
        $action_user = "脚本确认收货  by poptser";


        $order_sn = $_GET['ordersn'];

        if(strlen($order_sn)!=19||!is_numeric($order_sn)){
            exit("订单号错误");
        }

//        SELECT CONCAT("'",order_sn) FROM sdx_order_info WHERE
//  order_status !=2 AND pay_status IN (2,3)
//        AND shipping_status !=2  AND
//        add_time < 1519372800  AND `196_admin_id` = 0
//ORDER BY order_id DESC;


//
//
//        //订单数组
        $where = array(
            $order_sn
        );

        //循环处理
        foreach ($where as $value){
            $list=M('order_info')->field('order_id,order_sn,wid,user_id,order_status,shipping_status,pay_status,mobile,consignee,order_amount,money_paid,surplus,best_time')->
            where("order_sn='".$value."'")->find();
            if(empty($list)){
                echo $value." 不存在</br>";
                continue;
            }
            if($list['shipping_status']==2){
                echo $value." 已收货</br>";
                continue;
            }
            echo $value." 准备处理</br>";
            $data['order_status']=5;
            $data['shipping_status']=2;
            $data['confirm_time']=gmtime();
            $data['wp_userid']=$user_id;
            $id=M('order_info')->where(array('order_id'=>$list['order_id']))->save($data);

            if($id!=false) {
                $is_shipping_status = M("order_action")->
                where(array("order_id" => $list['order_id'], "shipping_status" => 2))
                    ->count();
                if (empty($is_shipping_status)) {
                    A("Distribution")->order_buckle_inventory($list);
                }
                $a = array(
                    'order_id' => $list['order_id'],
                    'user_id' => $user_id,
                    'order_status' => 5,
                    'shipping_status' => 2,
                    'pay_status' => $list['pay_status'],
                    'to_user' => 1,
                    'log_time'=>gmtime(),
                    'action_user' => $action_user,
                    'action_note' => '收货确认'
                );
                M('order_action')->add($a);//记录配送员操作日志
                $this->record_logistics($list['order_id'], 2, "确认收货");
                echo $value." 操作成功</br>";
            }
            //exit();
        }
    }
    /**
     * 写入订单物流信息
     * @param $order_id
     * @param $shipping_status
     * @param $remark
     */
    public function record_logistics($order_id, $shipping_status, $remark){
        $data['log_time']=gmtime();
        $data['remark']=$remark;
        $data['shipping_status']=$shipping_status;
        $data['order_id']=$order_id;
        M('logistics')->add($data);
    }
}